package top.xshiwu.demo.configure;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import top.xshiwu.demo.service.RDMessageEvent;
import top.xshiwu.demo.service.impl.MailServiceImpl;

import javax.mail.MessagingException;

/**
 * redis配置
 * 
 * @author ruoyi
 */
@Configuration
@EnableCaching
@Slf4j
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfig extends CachingConfigurerSupport
{

    /**
     * 邮件服务
     */
    @Autowired
    MailServiceImpl mailService;

    @Value("${to.qq}")
    String to;


    @Bean
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
    public RedisTemplate<Object, Object> redisTemplate( RedisConnectionFactory connectionFactory)
    {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);

        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        log.info("123456");

        return template;
    }


    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer( RedisConnectionFactory connectionFactory) throws Exception {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        log.info("654321");
        return container;
    }



    @Bean
   public MessageListener messageListener(RedisMessageListenerContainer container) {


        RDMessageEvent rdMessageEvent =   new RDMessageEvent(container);
        rdMessageEvent.addNotifyEmailListen(new RDMessageEvent.INotifyEmail() {
            @Override
            public void notify(Long projectId, String title, String content) throws MessagingException {
                log.info("to:" + to);
                final String[] toEmails = to.split(",");
                mailService.sendMail(toEmails,title,content);
            }
        });
        return rdMessageEvent;
   }




}
